<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class ApiException extends Exception
{
    /**
     * Create a new exception instance.
     */
    public function __construct(array $codeResponse, int $code = 500)
    {
        [$code,$message] = $codeResponse;
        parent::__construct($message, $code);
    }
    /**
     * Report the exception.
     */
    public function report()
    {
        return true;
    }

    /***
     * Render the exception into an HTTP response.
     */
    public function render(Request $request)
    {
        Log::error('ApiException-'.$this->getMessage().'：',[
            'type' => 'ApiException',
            'message' => $this->getMessage(),
            'code' => $this->getCode(),
            'request_url' => $request->url(),
            'request_method' => $request->method(),
            'request_ip' => $request->ip(),
            'request_headers' => $request->headers->all(),
            'request_query' => $request->query(),
            'request_body' => $request->all(),
            'user_id' => $request->user() ? $request->user()->id : null,
            'timestamp' => now()->toDateTimeString()
        ]);
        return response()->json([
            'code' => $this->getCode(),
            'msg' => $this->getMessage(),
            'data' => null
        ], 200);
    }
}
